<?php


namespace app\manage\controller;

use app\manage\model\Token;
use think\Db;
use think\Validate;
class Order
{
    public function __construct()
    {
        $this->token=new Token();
        $this->order=new \app\manage\model\Order();
        $this->express=new \app\applet\model\Express();
    }

    /**
     * 返利商城--订单--列表
     */
    public function rebateOrderList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->order->rebateOrderList($param,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    /**
     * 返利商城--分类列表
     */
    public function cateList(){
        $returnData=$this->order->cateList();
        returnJson(200,'success',$returnData);
    }

    /**
     * 返利商城--订单--批量删除
     */
    public function rebateDelAll(){
        $order_ids=input('order_ids');

        $this->order->shareDelAll($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     * 返利商城--订单--详情
     */
    public function rebateOrderDetail(){
        $order_ids=input('order_id');

        $data=$this->order->OrderDetail($order_ids,1);

        returnJson(200,'操作成功',$data);
    }

    /**
     * 爆品商城--订单--列表
     */
    public function explodeOrderList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->order->explodeOrderList($param,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    /**
     * 爆品商城--订单--批量删除
     */
    public function explodeDelAll(){
        $order_ids=input('order_ids');

        $this->order->shareDelAll($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     * 爆品商城--订单--详情
     */
    public function explodeOrderDetail(){
        $order_ids=input('order_id');

        $data=$this->order->OrderDetail($order_ids,2);

        returnJson(200,'操作成功',$data);
    }


    /**
     * 干货分享--订单--列表    1=热门（裂变）  2=精选（返利）
     */
    public function shareOrderList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];

        $returnData=$this->order->shareOrderList($param,$page,$page_size,$type);

        returnJson(200,'success',$returnData);
    }

    /**
     * 干货分享---订单--批量删除
     */
    public function shareDelAll(){
        $order_ids=input('order_ids');

        $this->order->shareDelAll($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     * 干货分享---订单--详情
     */
    public function shareOrderDetail(){
        $order_ids=input('order_id');

        $data=$this->order->OrderDetail($order_ids,3);

        returnJson(200,'操作成功',$data);
    }

    /**
     * 评价管理  type  2=待回复  1=已回复  3=全部
     */
    public function commentList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];

        $returnData=$this->order->commentList($param,$page,$page_size,$type);

        returnJson(200,'success',$returnData);

    }

    /**
     * 评价管理  回复页面
     */
    public function commentEdit(){
        $id=input('c_id');
        $data=$this->order->commentDetail($id);

        returnJson(200,'success',$data);
    }

    /**
     * 评价管理  回复
     */
    public function commentSubmit(){
        $param=input();
        $validate = new Validate([
            ['c_id', 'require', '评价id不能为空'],
            ['reply', 'require|max:300', '回复内容不能为空|回复内容长度不能超过300'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->order->commentReply($param);

        returnJson(200,'操作成功');
    }

    /**
     * 评价管理  删除
     */
    public function commentDel(){
        $c_id=input('c_id');

        $this->order->commentDel($c_id,1);

        returnJson(200,'操作成功');
    }

    /**
     * 评价管理  批量删除
     */
    public function commentDelAll(){
        $c_ids=input('c_ids');

        $this->order->commentDel($c_ids,2);

        returnJson(200,'操作成功');
    }

    /**
     * 订单管理  待发货
     */
    public function deliveredList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->order->orderList($param,$page,$page_size,1);

        returnJson(200,'success',$returnData);
    }

    /**
     * 订单管理  待发货--批量删除
     */
    public function deliveredDelAll(){
        $order_ids=input('order_ids');

        $this->order->shareDelAll($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     * 发货页面
     */
    public function delivery(){
        $order_id=input('order_id');

        $data=$this->order->deliveryPage($order_id);

        returnJson(200,'success',$data);

    }

    /**
     *  发货--提交
     */
    public function deliverGoods(){
        $param=input();

        $validate = new Validate([
            ['order_id', 'require', '订单id不能为空'],
            ['log_company', 'require', '物流公司不能为空'],
            ['log_sn', 'require', '物流单号不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->order->deliverGoods($param);

        returnJson(200,'提交成功');

    }

    /**
     *  退款
     */
    public function orderRefund(){
        $order_id=input('order_id');

        $this->order->orderRefund($order_id);

        returnJson(200,'操作成功');

    }

    /**
     * 订单管理  待收货---列表
     */
    public function receivingList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->order->orderList($param,$page,$page_size,2);

        returnJson(200,'success',$returnData);
    }

    /**
     * 订单管理  待收货--批量删除
     */
    public function receivingDelAll(){
        $order_ids=input('order_ids');

        $this->order->shareDelAll($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     *  提醒收货
     */
    public function receivingGoods(){
        $order_id=input('order_id');

        $this->order->receiving($order_id);

        returnJson(200,'操作成功');
    }

    /**
     *  查看物流
     */
    public function logistics(){
        $order_id=input('order_id');
        if(empty($order_id)){
            returnJson(-2004,'请传入order_id');
        }
        $order=Db::name("order")
            ->where(['order_id'=>$order_id])->find();
        $data['id']=$order['log_id'];
        $data['num']=$order['log_sn'];

        $data = $this->express->queryExpress($data);

        returnJson(200,"success",$data);


    }

    /**
     *  售后订单列表  type 1=全部  2=仅退款  3=退货退款  4=仅退货（暂时不要）
     */
    public function afterOrderList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];

        $returnData=$this->order->afterOrderList($param,$page,$page_size,$type);

        returnJson(200,'success',$returnData);
    }

    /**
     *  售后----批量删除
     */
    public function afterOrderDelAll(){
        $order_ids=input('oas_ids');

        $this->order->afterOrderDel($order_ids);

        returnJson(200,'操作成功');
    }

    /**
     *  售后----同意操作
     */
    public function afterAgree(){

        $param=input();
        $validate = new Validate([
            ['oas_id', 'require', '售后订单id不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $this->order->afterRefund($param,1);

        returnJson(200,'操作成功');

    }

    /**
     *  售后----拒绝操作
     */
    public function afterRefuse(){

        $param=input();
        $validate = new Validate([
            ['oas_id', 'require', '售后订单id不能为空'],
            ['refuse_reason', 'require', '拒绝原因不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }


        $this->order->afterRefund($param,2);

        returnJson(200,'操作成功');

    }


    public function test2(){
        $cate_id='3,13';
        //商品分类
        $cate=Db::name('choice_cate')
            ->field('name')
            ->where(['id'=>['in',$cate_id]])
            ->select();

        dump(substr($cate_id,0,strrpos($cate_id ,",")));
        dump(substr($cate_id,strripos($cate_id,",")+1));
        dump($cate);

        //returnJson(200,'success',$cate);

    }

    public function test(){
        $member_id=3;
        $goods_id=38;

        $order_sn=get_order1(1,1,$member_id);

        $data['member_id']=$member_id;
        $data['source_type']=4;
        $data['store_id']=0;
        $data['status']=5;
        $data['pay_status']=2;
        $data['order_sn']=$order_sn;
        $data['wx_money']='0.01';
        $data['order_score_money']='2000';
        $data['order_coin_money']='500';
        $data['receiver']='豆皮';
        $data['re_phone']='17012345678';
        $data['address']='四川省成都市温江区';
        $data['create_time']=time();

        $order_id=Db::name('order')->insertGetId($data);

        $info['order_id']=$order_id;
        $info['goods_id']=$goods_id;
        $info['goods_num']=1;
        $info['score_money']='2000';
        $info['coin_money']='500';
        $info['spec_name']='精选视频';
        $info['goods_name']='计算机基础学习路线';
        $info['delivery_fee']='0';
        $info['spec_id']=65;
        $info['spec_pic']='/20/06/22/15927928455481.jpg';
        //$info['copper']='30';
        $info['proportion']='5';
        $info['create_time']=time();

        Db::name('order_goods')->insert($info);


    }

}